import { defineStore } from "pinia"

export const useRouteStore = defineStore("route", {
  state: () => ({
    routeList: sessionStorage.getItem("routeList")
      ? JSON.parse(sessionStorage.getItem("routeList"))
      : [],
    isUpdate: false,
    activePath: "",
    tagList: sessionStorage.getItem("tagList")
      ? JSON.parse(sessionStorage.getItem("tagList"))
      : [],
    activeMenu: sessionStorage.getItem("activeMenu")
      ? JSON.parse(sessionStorage.getItem("activeMenu"))
      : [],
  }),
  actions: {
    updateRouteList(routeList) {
      this.routeList = routeList
      sessionStorage.setItem("routeList", JSON.stringify(routeList))
      this.isUpdate = true
    },
    addTag(tag) {
      this.tagList.push(tag)
      let map = new Map()
      for (let item of this.tagList) {
        if (!map.has(item.path)) {
          map.set(item.path, item)
        }
      }
      this.tagList = [...map.values()]
      sessionStorage.setItem("tagList", JSON.stringify(this.tagList))
    },
    deleteTag(index) {
      this.tagList.splice(index, 1)
      sessionStorage.setItem("tagList", JSON.stringify(this.tagList))
    },
    closeTag() {
      this.tagList = []
      sessionStorage.setItem("tagList", JSON.stringify(this.tagList))
    },
    setActiveMenu(activeMenu) {
      this.activeMenu = activeMenu
      sessionStorage.setItem("activeMenu", JSON.stringify(activeMenu))
    },
  },
  getters: {},
})
